package com.guo.dms.dept.user.utils;

import java.util.List;

import org.apache.shiro.SecurityUtils;

import com.guo.dms.common.menu.entity.Menu;
import com.guo.dms.common.shiro.entity.ShiroUser;
import com.guo.dms.dept.role.entity.Role;
import com.guo.dms.dept.user.entity.User;

/**
 * 用户工具
 * @author Administrator
 *
 */
public class UserUtils {
	
	public static User getCurrentUser(){
		 return getShiroUser().getUser();
	}
	
	public static List<Menu> getCurrentUserMenus(){
		return getShiroUser().getMenus();
	}
	

	public static List<Role> getCurrentUserRoles(){
		 
		return getShiroUser().getRoles();
	}
	
	private static ShiroUser getShiroUser(){
		if(isLogin()){
			return ((ShiroUser)SecurityUtils.getSubject().getPrincipal());
		}
		return null;
	}
	
	/**
	 * 是否具有权限
	 * @param permission
	 * @return
	 */
	public static boolean  isPermitted(String permission){
		if(isLogin()){
			return  SecurityUtils.getSubject().isPermitted(permission);
		}
		return false;
	}
	
	 public static boolean isLogin(){
		if(SecurityUtils.getSubject().isAuthenticated()){
			return true;
		}
		System.err.println("user is no login ! ");
		return false;
	 }
}
